<?php
/**
 * User: zj
 * Date: 2017/5/22
 * Time: 14:58
 * Progress:0%
 */

namespace logic\admin\shop;


use model\ShopRegUserOrder;

class LShopRegUserOrder
{

    //会员订单列表
    public function index_data($param){
        $model = new ShopRegUserOrder();
        $where=array();
        $status = $param['status'];
        if ($status > -1) {
            $where['s.flag_gift'] = $status;
        }
        $type = $param['type'];
        if ($type > -1) {
            $where['s.flag_pay'] = $type;
        }
        $keywords = $param['search'];
        if (!empty($keywords)) {
            $where['s.order_sn|a.name'] = array('like', "%{$keywords}%"); //查询条件
        }

        if(!empty($param['begindate'])){
            $begin_date = strtotime($param['begindate']);
        }else{
            $begin_date = '';
        }
        if(!empty($param['enddate'])){
            $end_date = strtotime('+1 day',strtotime($param['enddate']));
        }else{
            $end_date = '';
        }

        if (!empty($begin_date) && !empty($end_date)) {
            $where['s.order_time'] = array('between', "{$begin_date},{$end_date}");
        }else if(!empty($begin_date) && empty($end_date)){
            $where['s.order_time'] = array('egt', $begin_date);
        }else if(empty($begin_date) && !empty($end_date)){
            $where['s.order_time'] = array('elt', $end_date);
        }

        $total=$model->alias('s')
            ->join('shop_goods sg','sg.id = s.gift_goods_id','left')
            ->join('account a','a.id = s.account_id','left')
            ->where($where)
            ->count(); //记录总数

        $page_size =$param['limit']; //获取分页数
        $start = $param['offset']; //获取开始的记录数
        //获取排序字段------------------------------------------------------------开始

        $sort_name = $param['ordinal'];
        $sort_desc = $param['order'];

        if (!empty($sort_name)) {
            $sort[$sort_name] = $sort_desc;
        } else {
            $sort['s.order_time'] = 'desc'; //默认排序
        }
        //获取排序字段------------------------------------------------------------结束


        $rows = $model->alias('s')
            ->join('shop_goods sg','sg.id = s.gift_goods_id','left')
            ->join('account a','a.id = s.account_id','left')
            ->field('s.*,sg.name as goods_name,a.name as account_name')
            ->where($where)
            ->limit($start, $page_size)
            ->order($sort)
            ->select();

        $array=array(
            'total'=>$total,
            'rows'=>$rows
        );

        return $array;
    }

    public function detail_data($id){
        $model = new ShopRegUserOrder();
        $data = $model->alias('s')
            ->join('shop_goods sg','sg.id = s.gift_goods_id','left')
            ->join('account a','a.id = s.account_id','left')
            ->field('s.*,sg.name as goods_name,a.name as account_name')
            ->where(['s.id' => $id])
            ->find();
        return $data;
    }


    public function grant_gift_data($id){
        $model = new ShopRegUserOrder();
        $where['id'] = $id;
        $data  = $model->where($where)->field('id,gift_code')->find();
        return $data;
    }

    public function confirm_gift_data($param){
        $model = new ShopRegUserOrder();
        $where['id'] = $param['id'];
        $data['flag_gift'] = 1;
        $data['take_gift_time'] = time();
        $res = $model->where($where)->update($data);
        if($res){
            return array(
                'success'=>true,
                'message'=>'确认成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>$model->getError()
            );
        }
    }


}